package net.wanho.shiro.filter;

import net.wanho.async.AsyncFactory;
import net.wanho.async.AsyncTask;
import net.wanho.po.sys.User;
import net.wanho.shiro.service.LogoutService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter {


    @Autowired
    private LogoutService logoutService;

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {

        // 删除会话和缓存
        logoutService.deleteSessionAndCache();

        // 记录退出日志信息,异步记录
        User user =  (User) SecurityUtils.getSubject().getPrincipal();
        AsyncFactory.getInstance().schedule(AsyncTask.recordLogininfor(WebUtils.toHttp(request),user.getUserName(),"退出成功","0"));

        // 调用默认方法
        return super.preHandle(request, response);
    }

    @Override
    public String getRedirectUrl() {
        return "/login.html";
    }


}
